﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Reflection;

namespace StattiLibrary
{
    public class PluginManager
    {
        public static void AddPlugin(string plugin)
        {
            string path = Path.GetDirectoryName(plugin);
            string file = Path.GetFileName(plugin);
            if (!File.Exists(plugin))
                throw new ArgumentException("File {0} doesn't exists", file);
            //TODO: Validate plugin.
            File.Copy(plugin, Path.Combine(Directory.GetCurrentDirectory(), "Plugins/" + file), true);
        }

        public static List<Plugin> GetPlugins()
        {
            List<Plugin> result = new List<Plugin>();
            string pluginsPath = Path.Combine(Directory.GetCurrentDirectory(), "Plugins/");
            foreach (string file in Directory.GetFiles(pluginsPath))
            {
                Assembly currentPluginAssembly = Assembly.LoadFile(file);
                foreach (Type type in currentPluginAssembly.GetTypes())
                {
                    if(type.BaseType.Equals(typeof(IPlugin)))
                    {
                        IPlugin currentPlugin = (IPlugin)Activator.CreateInstance(type);
                        Plugin entry = new Plugin();
                        entry.Author = currentPlugin.Author;
                        entry.Description = currentPlugin.Description;
                        entry.Name = currentPlugin.Name;
                        entry.Version = currentPlugin.Version;
                        result.Add(entry);
                        break;
                    }
                }
            }
            return result;
        }

        public static void Post(string message, string username, string password)
        {
            string pluginsPath = Path.Combine(Directory.GetCurrentDirectory(), "Plugins/");
            foreach (string file in Directory.GetFiles(pluginsPath))
            {
                Assembly currentPluginAssembly = Assembly.LoadFile(file);
                foreach (Type type in currentPluginAssembly.GetTypes())
                {
                    if (type.IsPublic)
                    {
                        if (!type.IsAbstract)
                        {
                            if(type.GetInterface("StattiLibrary.IPlugin", true) != null)
                            {
                                IPlugin currentPlugin = (IPlugin)Activator.CreateInstance(type);
                                currentPlugin.Post(message, username, password);
                                break;
                            }
                        }
                    }
                }
            }
        }
    }

    public class Plugin
    {
        public Plugin() { }

        #region Properties
        public string Name
        {
            get;
            set;
        }

        public string Version
        {
            get;
            set;
        }

        public string Author
        {
            get;
            set;
        }

        public string Description
        {
            get;
            set;
        }
        #endregion
    }
}
